tempfile audjpy_basis
use "./OutputInterim/Basis_and_Returns.dta",clear
keep Date AUD_JPY_ois_spot_log_basis_3M
rename Date date
rename AUD_JPY_ois_spot_log_basis_3M audjpy_ois_3m
save `audjpy_basis'

*** Figure PC1 and the basis 
set scheme s1color
use "./OutputInterim/clean_arbitrage_basis_final.dta",clear
mmerge date using `audjpy_basis'
sort date
gen pc1_rescale=pc1/3.092349+1
replace audjpy_ois_3m=audjpy_ois_3m*100
label var pc1_rescale "First PC of Other Arbitrages (LHS)"
label var audjpy_ois_3m "Spot AUD-JPY 3M Basis (RHS)"

global xlab tlabel(1jan2006 1jan2008 1jan2010 1jan2012 1jan2014 1jan2016 1jan2018 1jan2020,format(%tdCY))

twoway (line pc1_rescale date,lcolor(blue) lpattern(dash) )(line audjpy_ois_3m date, lcolor(red)  yaxis(2)) ///
if year(date)>=2005, ylabel(-1(1)3.5) ylabel(-100(100)350, axis(2)) xtitle("") yline(0,lcolor(black)) ///
ytitle("Index of PC") ytitle("Basis Points", axis(2))  xsize(8) ysize(4) ylabel(,grid) $xlab legend(region(lwidth(none))) 
graph export "./OutputFinal/xccy_arbitrage_udpate.eps",replace


*** Intermediary constraints
use `audjpy_basis',clear
replace audjpy_ois_3m=audjpy_ois_3m*100
mmerge date using "./OutputInterim/He_Kelly_Manela_Factors_daily.dta", unmatched(master)
gen quarter=qofd(date)
format quarter %tq
sort date
bysort quarter: gen qe=1 if _n==_N
mmerge quarter using  "./OutputInterim/aem_update.dta", unmatched(master)
replace lev=. if qe!=1
replace hkm=hkm*100
label var audjpy_ois_3m "Spot AUD-JPY 3M Basis (RHS)"
sort date
gen cum_int_equity=sum(int_equity)
replace cum_int_equity=cum_int_equity*10

global xlab2 tlabel(1jan2004 1jan2006 1jan2008 1jan2010 1jan2012 1jan2014 1jan2016 1jan2018 1jan2020,format(%tdCY))

twoway (line  hkm cum_int_equity lev  date, lcolor(blue green orange) lpattern(dot_dash dash longdash))(line audjpy_ois_3m date, lcolor(red) yaxis(2)), ///
yline(0,axis(2) lcolor(black)) ylabel(-10(10)40)  ///
ytitle("Basis Points", axis(2)) ytitle("Capital Ratio / Return Index") xtitle("") xsize(8) ysize(4) ylabel(,grid) $xlab2 ///
legend(region(lwidth(none)) row(2) order(1 "HKM Capital Ratio (LHS)" 2 "Scaled Intermediary Equity Return Index (LHS)" 3 "AEM Leverage Ratio (LHS)" 4 "Spot AUD-JPY 3M Basis (RHS)" ))
graph export "./OutputFinal/Factor_Return_Correlations_update.eps",replace

